﻿namespace LxCFD
{
    public static class ComputeState
    {
        private static ComputeStates _ComputeState = ComputeStates.NotRunning;
        private static volatile bool _SettingChanged = true;
        private static readonly object _LocalLock = new object();
        public static bool IsNeedInterrupt()
        {
            lock (_LocalLock)
            {
                return _ComputeState == ComputeStates.Running_InterruptByUser || _ComputeState == ComputeStates.Running_InterruptByError;
            }
        }
        public static bool IsSettingChanged()
        {
            lock (_LocalLock)
            {
                return _SettingChanged;
            }
        }
        public static void ReportSettingChanged()
        {
            lock (_LocalLock)
            {
                if (!_SettingChanged)
                {
                    _SettingChanged = true;
                }
            }
        }
        public static void ReportSettingNoticed()
        {
            lock (_LocalLock)
            {
                if (_SettingChanged)
                {
                    _SettingChanged = false;
                }
            }
        }
        public static bool SetIsRunning(bool isRunning)
        {
            lock (_LocalLock)
            {
                if (isRunning)
                {
                    if (_ComputeState != ComputeStates.NotRunning) return false;
                    _ComputeState = ComputeStates.Running;
                }
                else
                {
                    _ComputeState = ComputeStates.NotRunning;
                }
            }
            return true;
        }
        public static void ReportInterrupt_ByUser()
        {
            lock (_LocalLock)
            {
                if (_ComputeState == ComputeStates.Running)
                {
                    _ComputeState = ComputeStates.Running_InterruptByUser;
                }
            }
        }
        public static void ReportInterrupt_FindError()
        {
            lock (_LocalLock)
            {
                if (_ComputeState == ComputeStates.Running)
                {
                    _ComputeState = ComputeStates.Running_InterruptByError;
                }
            }
        }
        public static bool GetIsRunning()
        {
            lock (_LocalLock)
            {
                return _ComputeState != ComputeStates.NotRunning;
            }
        }
        public enum ComputeStates
        {
            NotRunning,
            Running,
            Running_InterruptByUser,
            Running_InterruptByError,
        }
    }

}
